//index.js
//获取应用实例
const app = getApp()

Page({
  onReady: function () {
    // Do something when page ready.
    console.log("Do something when page ready.")
  },
  onShow: function () {
    // Do something when page show.
    console.log("Do something when page show.")
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onReachBottom: function () {
    // Do something when page reach bottom.
  },

  onPageScroll: function (object) {
    // Do something when page scroll
    console.log(object.scrollTop)
  },
  onShareAppMessage:function () {
    return {
      title:'转发的标题',
      // path:'pages/index/index'
      path: 'pages/logs/logs'
    }
  },
  data: {
    motto: 'Hello World 这就是第一个小程序',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function(event) {

    wx.request({
      url: 'http://10.1.22.228:8888/mockjsdata/7/user/myFavQuestions', //仅为示例，并非真实的接口地址
      data: {
        x: '',
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
      }
    })


    console.log('view tap')
    console.log(event)
    console.log(getApp().globalData)
    // wx.navigateTo({
    //   url: '../logs/logs',
    // })
    wx.switchTab({
      url: '../logs/logs',
    })
  },
  
  onPullDownRefresh: function () {
    console.log('刷新了，我擦')
   
    wx.showToast({
      title: '成功',
      icon: 'success',
      duration: 2000
    })
    wx.stopPullDownRefresh()
    wx.showModal({
      title: '提示',
      content: '这是一个模态弹窗',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onReachBottom:function (){
    wx.showModal({
      title: '这是title',
      content: '这是内容',
    })
  },
  buttonClick: function(){
    wx.startPullDownRefresh({})
    console.log('点击了按钮')
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.model)
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        console.log(res.platform)
      }
    })
  },
  buttonClick2: function () {
    wx.makePhoneCall({
      phoneNumber: '18810901468' //仅为示例，并非真实的电话号码
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
